import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.Coordinate;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;


public class CoordTransform {
    public static void main(String[] args) throws FactoryException, TransformException {

        Coordinate coord = new Coordinate(328443.4399, 345557.6179);
        FourParams fourParams = new FourParams(3160419.114543, 20768910.344964, -1.0687170034, 0.838653405643);
        double deltaX = fourParams.deltaX;
        double deltaY = fourParams.deltaY;
        double k = fourParams.k;
        double t = fourParams.t;

        // 平面坐标 => 投影坐标(new beijing)
        double x = coord.x;
        double y = coord.y;
        double x2 = deltaX + x * k * Math.cos(t) - y * k * Math.sin(t);
        double y2 = deltaY + y * k * Math.cos(t) + x * k * Math.sin(t);

        // 投影坐标(new beijing) => 地理坐标(new beijing)
        Coordinate coord_1 = transformBySRID(new Coordinate(x2, y2), "EPSG:4575", "EPSG:4555");

        // 地理坐标(new beijing) => 地理坐标(wgs1984)
        Coordinate coord_2 = transformBySRID(coord_1, "EPSG:4555", "EPSG:4326");

        System.out.println(coord_1.x + "," + coord_1.y);
        System.out.println(coord_2.x + "," + coord_2.y);
    }

    /**
     * 根据编码转换坐标
     * @param coord      坐标
     * @param sourceSRID 源坐标编码
     * @param targetSRID 目标坐标编码
     * @return Coordinate
     * @throws FactoryException
     * @throws TransformException
     */
    public static Coordinate transformBySRID(Coordinate coord, String sourceSRID, String targetSRID) throws FactoryException, TransformException {

        CoordinateReferenceSystem sourceCRS = CRS.decode(sourceSRID);
        CoordinateReferenceSystem targetCRS = CRS.decode(targetSRID);
        MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, true);
        Coordinate sourceCoord = coord;
        Coordinate targetCoord = new Coordinate();
        JTS.transform(sourceCoord, targetCoord, transform);
        return targetCoord;
    }


}
